/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.icybear.gae.pechapaii;

import java.net.URLDecoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import net.icybear.gae.pechapaii.service.TwitterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;

/**
 *
 * @author Bearice
 */
@Controller
@RequestMapping("/tweet.wml")
public class Tweet {

    @Autowired
    private TwitterService twitterService;

    @RequestMapping(params = "id")
    public String show(HttpServletRequest request, long id) {
        try {
            Twitter twitter = twitterService.createTwitter(request);
            if (twitter == null) {
                return "wml/login";
            }
            Status tweet = twitter.showStatus(id);
            request.setAttribute("tweet", tweet);
            request.setAttribute("rateLimitStatus", twitter.getRateLimitStatus());
            return "wml/tweet";
        } catch (TwitterException ex) {
            request.setAttribute("message", ex);
            return "wml/error";
        }
    }

    @RequestMapping(params = {"id", "action=reply"})
    public String reply(HttpServletRequest request, long id, String name) {
        Twitter twitter = twitterService.createTwitter(request);
        if (twitter == null) {
            return "wml/login";
        }
        request.setAttribute("text", "@" + name + " ");
        request.setAttribute("reply_to", id);
        return "wml/tweet_new";
    }

    @RequestMapping(params = {"id", "action=retweet"})
    public String retweet(HttpServletRequest request, long id) {
        try {
            Twitter twitter = twitterService.createTwitter(request);
            if (twitter == null) {
                return "wml/login";
            }
            Status tweet = twitter.showStatus(id);
            request.setAttribute("text", "RT @" + tweet.getUser().getScreenName() + " " + tweet.getText());
            request.setAttribute("reply_to", id);
            request.setAttribute("retweet", true);
            return "wml/tweet_new";
        } catch (TwitterException ex) {
            request.setAttribute("message", ex.getMessage());
            return "wml/error";
        }
    }

    @RequestMapping(params = {"id", "action=retweet", "offical=true"})
    public String retweet_offical(HttpServletRequest request, long id) {
        try {
            Twitter twitter = twitterService.createTwitter(request);
            if (twitter == null) {
                return "wml/login";
            }
            Status tweet = twitter.showStatus(id);
            request.setAttribute("tweeet", tweet);
            request.setAttribute("rateLimitStatus", twitter.getRateLimitStatus());
            request.setAttribute("message", "Status retweeted");
            return "wml/tweet";
        } catch (TwitterException ex) {
            request.setAttribute("message", ex.getMessage());
            return "wml/error";
        }
    }

    @RequestMapping(method = RequestMethod.POST, params = "action=update")
    public String update(String status, HttpServletRequest request) {
        return update(status, -1, request);
    }

    @RequestMapping(method = RequestMethod.POST, params = {"reply_to", "action=update"})
    public String update(String text, long reply_to, HttpServletRequest request) {
        Twitter twitter = twitterService.createTwitter(request);
        if (twitter == null) {
            return "wml/login";
        }
        try {
            text = URLDecoder.decode(text, "utf-8");
            twitter.updateStatus(text, reply_to);
        } catch (Exception ex) {
            request.setAttribute("message", ex.getMessage());
            return "wml/error";
        }
        return "redirect:timeline.wml";
    }

    @RequestMapping(params = {"id", "action=del"})
    public String del(HttpServletRequest request, long id) {
        try {
            Twitter twitter = twitterService.createTwitter(request);
            if (twitter == null) {
                return "wml/login";
            }
            Status tweeet = twitter.destroyStatus(id);
            request.setAttribute("tweeet", tweeet);
            request.setAttribute("rateLimitStatus", twitter.getRateLimitStatus());
            request.setAttribute("message", "Status deleted");
            return "wml/tweet";
        } catch (TwitterException ex) {
            request.setAttribute("message", ex);
            return "wml/error";
        }
    }
}
